package com.speech.utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.speech.R;

public class TitleBuilder {

    private TextView tv_title;
    private ImageView img_leftIcon;

    private TextView tv_rightView;

    private Activity mContext;

    public TitleBuilder(Activity context){
        mContext = context;
        tv_title = context.findViewById(R.id.tv_component_title_name);
        tv_rightView = context.findViewById(R.id.tv_component_title_right_view);

        img_leftIcon = context.findViewById(R.id.img_component_title_back);

        setLeftIcoListening(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //默认功能是关闭当前activity
                mContext.finish();
            }
        });
    }

    /**
     * 用于设置标题栏文字
     * */
    public TitleBuilder setTitleText(String strTitle){
        if(!TextUtils.isEmpty(strTitle)){
            tv_title.setText(strTitle);
        }
        return this;
    }

    /**
     * 用于设置标题栏左边要显示的图片
     * */
    public TitleBuilder setLeftIco(int resId){
        img_leftIcon.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        img_leftIcon.setImageResource(resId);
        return this;
    }

    /**
     * 用于设置标题栏右边TextView文本内容
     * */
    public TitleBuilder setRightTextViewContent(String strContent){
        tv_rightView.setText(strContent);
        return this;
    }

    /**
     * 用于设置标题栏左边图片的单击事件
     * */
    public TitleBuilder setLeftIcoListening(View.OnClickListener listener){
        if(img_leftIcon.getVisibility() == View.VISIBLE){
            img_leftIcon.setOnClickListener(listener);
        }
        return this;
    }

    /**
     * 用于设置标题栏右边TexVtiew的单击事件
     * */
    public TitleBuilder setRightViewListening(View.OnClickListener listener){
        if(tv_rightView.getText().length()>0){
            tv_rightView.setOnClickListener(listener);
        }
        return this;
    }

}
